package cn.wolfcode.cloud.shop.resp;

import lombok.*;

import java.io.Serializable;

/**
 * @author Administrator
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Result<T> implements Serializable {

    //默认成功状态码
    public static final Integer DEFAULT_SUCCESS_CODE = 0;

    //默认失败状态码
    public static final Integer DEFAULT_SERVER_ERROR_CODE = 50000500;

    //默认成功信息
    public static final String DEFAULT_SUCCESS_MSG = "操作成功";

    //默认失败信息
    public static final String DEFAULT_SERVER_ERROR_MSG = "服务器繁忙,请稍后重试";

    private Integer code;
    private String msg;
    private T data;

    public Boolean hasError(){
        return this.getCode() != DEFAULT_SUCCESS_CODE;
    }

    public static <T> Result<T> success(){
        return success(null);
    }

    public static <T> Result<T> success(T data){
        return new Result<>(DEFAULT_SUCCESS_CODE,DEFAULT_SUCCESS_MSG,data);
    }

    public static <T> Result<T> error(){
        return new Result<>(DEFAULT_SERVER_ERROR_CODE,DEFAULT_SERVER_ERROR_MSG,null);
    }

    public static <T> Result<T> error(CodeMsg codeMsg){
        return new Result<T>(codeMsg.getCode(),codeMsg.getMsg(),null);
    }


}
